package _2025_3_26;

public class MyBlockingDeque {
 //使用数组模拟
    private int[]elem;
      public MyBlockingDeque(int sum){
          if(sum<=0){
              throw new IllegalArgumentException("队列大小不能<0");
          }else{
              elem=new int[sum];
          }
      }
     private volatile int size=0;
      private volatile int head=0;//记录插入节点
      public int tail=0;//记录删除节点
      public void put(int value) throws InterruptedException {
          synchronized (this) {
              if (size > elem.length) {
                  //阻塞，使其等待 this当前这个
                  this.wait();
              }
              elem[head++] = value;
              if (head == elem.length) {
                  head = 0;
              }
              size++;
          }
      }
      public synchronized int take() throws InterruptedException {
          if (size==0){
              this.wait();
          }
          int key=elem[tail++];
          if(tail==elem.length){
              tail=0;
          }
          size--;
          //唤醒等待线程
          this.notifyAll();
          return key;
      }
}
